<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller{
	protected $params = array();
	
	function MY_Controller($redirect=true){
		parent::__construct();
		
		$lang = Sys::getLang();
		$this->params['lang_url']	= $this->get2Lan();
		if($redirect){
			if($lang == 'eng' && $this->params['lang_url'] == '')
				header('Location: '.Sys::$url_base.'en/');
		}
		
		$this->getParams();
		
		
		Sys::loadLanguage($lang, 'view');
		Sys::loadLanguage($lang, 'text');
		$this->params['lang'] 		= $lang;
		
		$this->load->helper(array('form', 'url'));
		$this->load->library('carabiner');
		$this->carabiner->config(
			array(
			    'base_uri'   => base_url(),
			    'combine'    => TRUE,
			    'dev'        => FALSE
		));
	}
	
	
	private function get2Lan(){
		if($this->uri->segment(1) !== FALSE){
			switch ($this->uri->segment(1)) {
				case 'en':
					return 'en/';
				break;
			}
		}
		return '';
	}
	
	private function getParams(){
		$this->params['consumer_key'] = Sys::$consumer_key;
		$this->params = array_merge($this->params, $_POST, $_GET);
		
		foreach ($this->params as $key => $value)
    		$this->params[$key] = Sys::limpiarTexto($this->security->xss_clean($value));
	}
	
	
	/**
	 * Regresa los subsegmentos que estan representados por el caracter ~
	 * Si se espesifica $index regresa el valor de ese segmento,
	 * de lo contrario regresa todos un array
	 */
	protected function subSegment($index=NULL){
		$segments = explode('~', $this->uri->segment($this->uri->total_segments()));
		
		if($index != NULL){
			if(isset($segments[$index]))
				return $segments[$index];
			return '';
		}
		return $segments;
	}

	public function checkAccessToken(){
		$this->params['oauth_token'] = Sys::getTokenVal();
		
		if($this->params['oauth_token']!=FALSE){
			
			$result = $this->config->item('store_oauth')->checkAccessToken(
						$this->params['consumer_key'], 
						$this->params['oauth_token']);
			if($result)
				return TRUE;
			else 
				return FALSE;
		}
		else return FALSE;
	}
}
?>